<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceSendEmailGuestList extends core {

    public function process() {
        $user_id = intval($_POST['user_id']);
        $data_user = $this->db->get_row("SELECT * FROM users WHERE `id`='$user_id'");
        if ($data_user) {
            $user_email = $data_user->email;
            $data = $this->db->get_results("SELECT * FROM guests WHERE user_id={$user_id} AND is_invite = 1  ORDER BY firstname asc;");
            $messages = '';
            foreach ($data as $guest) {
                $city_state_zipcode='';
                if($guest->city!==""){
                    $city_state_zipcode.=$guest->city;
                }
                if($guest->state!==""){
                    if($city_state_zipcode ==""){
                        $city_state_zipcode.=$guest->state;
                    }else{
                        $city_state_zipcode.=' - '.$guest->state;
                    }
                }
                if($guest->zipcode!==""){
                    if($city_state_zipcode ==""){
                        $city_state_zipcode.=$guest->zipcode;
                    }else{
                        $city_state_zipcode.=' - '.$guest->zipcode;
                    }
                }
                $address='';
                    if ($guest->address != "" || $city_state_zipcode != "") {
                        $address.='<table style="font-family:Arial,Sans-serif;font-size: 14px;" cellspacing="0" cellpadding="0" border="0" >';

                        if ($guest->address != "") {
                            $address.='<tr><td valign="top">' . $guest->address . '</td></tr>';
                        }
                        if ($city_state_zipcode != "") {
                            $address.='<tr><td valign="top" ><span><span>' . $city_state_zipcode . '</span></span></td></tr>';
                        }
                        $address.='</table>';
                    }
                $messages .='<table cellspacing="0" cellpadding="8" border="0"  style="width:100%;font-family:Arial,Sans-serif;font-size: 14px;border-width:1px 2px 2px 1px;" ><tr><td><div style="padding:2px">' . $guest->firstname . ' ' . $guest->lastname .$address.'</div></td></tr></table>';
            }
            $subject = "Your Wedding Helper contact list";
            if (core::send_mail($user_email, $subject, $messages)) {
                 $this->_JSONResponse(array('msg' => "Contact list has been sent. Please check your email"));
            }else{
                $this->_JSONResponse(array('msg' => "Oops, something went wrong. Please try again"));
            }
        }else{
            $this->_JSONResponse(array('msg' => "User does not exist!"));
        }
      
    }

}

$processor = new webServiceSendEmailGuestList();
$processor->process();
?>